package cn.itcast.opengl;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;

import java.lang.reflect.Field;

/**
 * activity
 */
public class OpenglActivity extends Activity {
	float ratio = 0 ;
	
	//gl表层画面
	private MyGLSurfaceView view;

	//renderer
	private AbstractRenderer renderer;

	private Dialog d;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = new MyGLSurfaceView(this);
        
        //设置EGL配置选择器,指定rgb,深度和模板深度值
        //view.setEGLConfigChooser(5,6,5,0,16, 4);//
        //
        renderer = new MyRendererTex(this.getResources());
        //渲染模式:持续渲染(默认)|命令渲染
        view.setRenderer(renderer); 
        //设置脏渲染(命令渲染)
        view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        this.setContentView(view);
        //奇门遁甲
        //设置该view可以获得焦点
        view.setFocusable(true);
        //view请求获得焦点
        view.requestFocus();
        
        //构建对话框
        buildDialoag();
    }
    
    
	class MyGLSurfaceView extends GLSurfaceView{
		public MyGLSurfaceView(Context context) {
			super(context);
		}
		
		/**
	     * 是否传播,surfaceView控件本身响应按键信息
	     */
	    public boolean onKeyDown(int keyCode, KeyEvent event) {
	    	//向下按键
	    	float step = 3f ;
	    	if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
	    		renderer.mxAngle = renderer.mxAngle + step ;
	    	}
	    	else if(keyCode == KeyEvent.KEYCODE_DPAD_UP){
	    		renderer.mxAngle = renderer.mxAngle - step ;
	    	}
	    	else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
	    		renderer.myAngle = renderer.myAngle - step ;
	    	}
	    	else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
	    		renderer.myAngle = renderer.myAngle + step ;
	    	}
	    	view.requestRender();
	    	return super.onKeyDown(keyCode, event);
	    }
    }
    
    public void click(View v){
    	//请求渲染
    	view.requestRender();
    }
    
    /**
     * 让view的周期和activity周期做一致性响应
     */
    protected void onResume() {
    	super.onResume();
    	view.onResume();
    }
    
    protected void onPause() {
    	super.onPause();
    	view.onPause();
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0, 100, 0, "配置");
    	return true;
    }
    
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	if(item.getItemId() == 100){
    		d.show();
    	}
    	return true ;
    }
    

    /**
     * 复选框选中监听
     */
    CompoundButton.OnCheckedChangeListener cbl = new  CompoundButton.OnCheckedChangeListener(){
		public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
			int id = buttonView.getId();
			//启用光照
			if(id == R.id.cbEnableLighting){
				renderer.enable_lighting = isChecked;
			}
			//启用颜色追踪
			else if(id == R.id.cbColorMaterrial){
				renderer.enable_color_material = isChecked ;
			}
			
			//启用重算法线规范化
			else if(id == R.id.cbRescaleNormals){
				renderer.enable_rescale_normals = isChecked ;
			}
			
			//渲染场景
			view.requestRender();
		}
    };
    
    //滑动条改变监听
    SeekBar.OnSeekBarChangeListener barl = new SeekBar.OnSeekBarChangeListener(){
		public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
			float percent = (float)progress / seekBar.getMax() ;
			int id = seekBar.getId() ;
			/***************  全局环境光  *************/
			//red
			if(id == R.id.bar_glbal_ambient_r){
				renderer.global_ambient_r = percent ;
			}
			//green
			else if(id == R.id.bar_glbal_ambient_g){
				renderer.global_ambient_g = percent ;
			}
			//blue
			else if(id== R.id.bar_glbal_ambient_b){
				renderer.global_ambient_b = percent ;
			}
			
			/***************  材料的环境光*散射光的反射率  *************/
			//环境和散射光反射率
			else if(id == R.id.bar_material_ambient_and_diffuse_r){
				renderer.material_ambient_and_diffuse_r = percent ;
			}
			else if(id == R.id.bar_material_ambient_and_diffuse_g){
				renderer.material_ambient_and_diffuse_g = percent ;
			}
			else if(id == R.id.bar_material_ambient_and_diffuse_b){
				renderer.material_ambient_and_diffuse_b = percent ;
			}
			//镜面光反射率
			else if(id == R.id.bar_material_specular_r){
				renderer.material_specular_r = percent ;
			}
			else if(id == R.id.bar_material_specular_g){
				renderer.material_specular_g = percent ;
			}
			else if(id == R.id.bar_material_specular_b){
				renderer.material_specular_b = percent ;
			}
			
			/***************  color设置  *************/
			else if(id == R.id.bar_color_r){
				renderer.color_r = percent ;
			}
			else if(id == R.id.bar_color_g){
				renderer.color_g = percent ;
			}
			else if(id == R.id.bar_color_b){
				renderer.color_b = percent ;
			}
			
			/***************  光源0  *************/
			//环境光
			else if(id == R.id.bar_light0_ambient_r){
				renderer.light0_ambient_r = percent ;
			}
			else if(id == R.id.bar_light0_ambient_g){
				renderer.light0_ambient_g = percent ;
			}
			else if(id == R.id.bar_light0_ambient_b){
				renderer.light0_ambient_b = percent ;
			}
			//散射光
			else if(id == R.id.bar_light0_diffuse_r){
				renderer.light0_diffuse_r = percent ;
			}
			else if(id == R.id.bar_light0_diffuse_g){
				renderer.light0_diffuse_g = percent ;
			}
			else if(id == R.id.bar_light0_diffuse_b){
				renderer.light0_diffuse_b = percent ;
			}
			//镜面光
			else if(id == R.id.bar_light0_specular_r){
				renderer.light0_specular_r = percent ;
			}
			else if(id == R.id.bar_light0_specular_g){
				renderer.light0_specular_g = percent ;
			}
			else if(id == R.id.bar_light0_specular_b){
				renderer.light0_specular_b = percent ;
			}
			//位置
			else if(id == R.id.bar_light0_pos_x 
					||id == R.id.bar_light0_pos_y 
					||id == R.id.bar_light0_pos_z){
				float pos = -10 + progress;
				if(id == R.id.bar_light0_pos_x){
					renderer.light0_pos_x = pos ;
				}
				else if(id == R.id.bar_light0_pos_y){
					renderer.light0_pos_y = pos ;
				}
				else if(id == R.id.bar_light0_pos_z){
					renderer.light0_pos_z = pos ;
				}
			}
			
			view.requestRender();
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
		}
		public void onStopTrackingTouch(SeekBar seekBar) {
		}
    };
    /**
     * 构建对话框
     */
    private void buildDialoag() {
    	try {
			d = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
			d.setContentView(R.layout.conf);
			
			//动态添加所有的滑动条监听
			Class clazz = R.id.class ;
			Field[] fs = clazz.getFields();
			Integer id  = null ;
			View view ;
			for(Field f : fs){
				id = (Integer) f.get(null);
				view = d.findViewById(id);
				if(view != null){
					if(view instanceof SeekBar){
						((SeekBar)view).setOnSeekBarChangeListener(barl);
					}
					else if(view instanceof CheckBox){
						((CheckBox)view).setOnCheckedChangeListener(cbl);
					}
				}
			}
			SeekBar bar = (SeekBar) d.findViewById(R.id.bar_glbal_ambient_r);
			bar.setOnSeekBarChangeListener(barl);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}